package com.sum.tea.ui

import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.Toast
import androidx.annotation.ArrayRes
import androidx.annotation.StringRes
import com.sum.common.bean.LockParams
import com.sum.tea.base.BaseMvvmFragment
import com.sum.common.bean.CommonResult
import com.sum.tea.R
import com.sum.tea.databinding.ActivityLockBinding
import com.sum.tea.viewmodel.LockViewModel

class LockFragment : BaseMvvmFragment<ActivityLockBinding, LockViewModel>() {

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initSpinners()
        initClickListeners()
    }

    private fun initSpinners() {
        setupSpinner(
            spinner = mBinding.snLockregoin,
            arrayResId = R.array.spilockbank,
            defaultSelection = 2
        )

        setupSpinner(
            spinner = mBinding.snLock,
            arrayResId = R.array.spilocktype
        )
    }

    private fun setupSpinner(
        spinner: Spinner,
        @ArrayRes arrayResId: Int,
        defaultSelection: Int = 0
    ) {
        ArrayAdapter.createFromResource(
            requireContext(),
            arrayResId,
            android.R.layout.simple_spinner_item
        ).apply {
            setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spinner.adapter = this
            spinner.setSelection(defaultSelection)
        }
    }

    private fun initClickListeners() {
        mBinding.btnLock.setOnClickListener { handleLock() }
    }

    private fun handleLock() {
        with(mBinding) {
            val params = LockParams(
                etAccesspwd.text.toString(),
                snLockregoin.selectedItemPosition,
                snLock.selectedItemPosition
            )
            mViewModel.lockTag(params)
        }
    }
}